
import { _decorator, Component, Node, Animation, UITransform, Size, Vec2, Vec3 } from 'cc';
import { SkillOrigin } from './SkillOrigin';
import { ConfigKey, secondGun_specialSkill_config } from '../../manager/AppControll';
import { DamageStruct } from '../FightControl';
import { MonsterOrigin } from '../monster/MonsterOrigin';
import { SoundManager } from '../../manager/SoundManger';
import { DataHelper } from '../../system/DataHelper';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = Skill
 * DateTime = Fri May 19 2023 14:49:27 GMT+0800 (GMT+08:00)
 * Author = guoxun
 * FileBasename = Skill.ts
 * FileBasenameNoExtension = Skill
 * URL = db://assets/script/fight/skill/Skill.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('SkillLightning')
export class SkillLightning extends SkillOrigin {
    private isStart = false;
    private mStartPoint: Vec2 = null;
    private mTarget: Node = null;
    InitSkill(config: secondGun_specialSkill_config, dmg: DamageStruct): void {
        super.InitSkill(config, dmg);
        this.InitAnimation(this.PlayAnimaition.bind(this));
    }

    InitAnData(startPoint: Vec2, mTarget: Node) {
        this.mTarget = mTarget;
        this.mStartPoint = startPoint;
    }

    PlayAnimaition() {
        if (!this.mTarget || !this.mTarget.position || !this.mSikllConfig) {
            this.node.destroy();
            return;
        }
        this.isStart = true;
        let hight = new Vec2(this.mTarget.position.x, this.mTarget.position.y).clone().subtract(this.mStartPoint).length() + 80;
        this.node.getComponent(UITransform).contentSize = new Size(hight / 8.4, hight);
        let direction = new Vec3(this.mStartPoint.x, this.mStartPoint.y, 0).subtract(this.mTarget.position).normalize();
        let angle = new Vec2(1, 0).angle(new Vec2(direction.x, direction.y));
        this.node.eulerAngles = new Vec3(0, 0, -angle / Math.PI * 180 + 90);
        this.node.getComponent(Animation).on(Animation.EventType.FINISHED, () => {
            this.isStart = false;
            this.node.getComponent(Animation).stop();
            this.node.destroy();
        });
        
        this.mTarget.getComponent(MonsterOrigin).Attacked(this.mDmg);
        if (this.mSikllConfig.aniAudio != "") {
            SoundManager.Instance<SoundManager>().PlayEffect("gun/" + this.mSikllConfig.aniAudio, Number.parseFloat(DataHelper.GetConfigByKey(ConfigKey.gunVolume).value));
        }
        this.node.getComponent(Animation).play(this.mSikllConfig.skillAni);
    }

    protected update(dt: number): void {
        if (!this.isStart) return;
        if (!this.mTarget) return;
        if (!this.mTarget.position) return;
        let hight = new Vec2(this.mTarget.position.x, this.mTarget.position.y).clone().subtract(this.mStartPoint).length() + 80;
        this.node.getComponent(UITransform).contentSize = new Size(hight / 8.4, new Vec2(this.mTarget.position.x, this.mTarget.position.y).clone().subtract(this.mStartPoint).length() + 80);
        let direction = new Vec3(this.mStartPoint.x, this.mStartPoint.y, 0).subtract(this.mTarget.position).normalize();
        let angle = new Vec2(1, 0).angle(new Vec2(direction.x, direction.y));
        this.node.eulerAngles = new Vec3(0, 0, -angle / Math.PI * 180 + 90);
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
